﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;
using System.Text;
using System.Text.RegularExpressions;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class HuodongController : BaseController
    {


        [Description("活动列表")]
        [ViewPage]
        public ActionResult GetList(HuodongQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_HuodongMaster.Where(t => t.is_del == 0);
            if (queryModel.huodong_status != -10)
            {
                queryable = queryable.Where(t => t.huodong_status == queryModel.huodong_status);
            }

            if (!string.IsNullOrWhiteSpace(queryModel.huodong_title))
                queryable = queryable.Where(t => (t.huodong_title.Contains(queryModel.huodong_title)));

            if (queryModel.beg_time.HasValue)
                queryable = queryable.Where(t => t.begin_time >= queryModel.beg_time);
            if (queryModel.end_time.HasValue)
            {
                var end_time = queryModel.end_time.Value.AddSeconds(1);
                queryable = queryable.Where(t => t.begin_time < end_time);
            }

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.huodong_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_HuodongMaster> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_HuodongMaster>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("编辑活动")]
        [ViewPage]
        public ActionResult Add(long id = 0)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_HuodongMaster model = new ADZ_HuodongMaster();
            if (id > 0)
            {
                model = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == id && t.is_del == 0);
            }
            return View(model);
        }

        [Description("编辑活动")]
        [HttpPost]
        public ActionResult Add(ADZ_HuodongMaster model)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            //参数验证
            if (string.IsNullOrWhiteSpace(model.huodong_title))
            {
                resMsg.Message = "活动标题不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (model.begin_time == DateTime.MinValue)
            {
                resMsg.Message = "请设置正确的活动开始时间";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if(model.end_time.HasValue && model.end_time <= model.begin_time)
            {
                resMsg.Message = "请设置正确的活动开始结束时间";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }
            if (string.IsNullOrWhiteSpace(model.huodong_copy_title))
                model.huodong_copy_title = "";

            if (string.IsNullOrWhiteSpace(model.huodong_content))
            {
                resMsg.Message = "活动描述信息不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            if (hfc != null && hfc.Count > 0)
            {
                var file = hfc[0];
                var putResult = ImageFileBiz.UpImg(file, "huodong");
                if (putResult.Item1)
                    model.img_url = putResult.Item3;
            }
            else
            {
                if (string.IsNullOrWhiteSpace(model.img_url))
                    model.img_url = "";
            }

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (model.huodong_id > 0)
            {
                var old_model = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == model.huodong_id);
                if (old_model != null)
                {
                    List<string> explist = new List<string>();
                    explist.Add("create_time");
                    explist.Add("is_del");
                    explist.Add("huodong_id");
                    Utils.Mapper(old_model, model, explist);
                }

                dbcontext.SaveChanges();

            }
            else
            {
                model.create_time = DateTime.Now;
                dbcontext.ADZ_HuodongMaster.Add(model);
            }

            dbcontext.SaveChanges();

            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("删除活动")]
        [HttpPost]
        public ActionResult ToDel(int id = 0)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var entity = dbcontext.ADZ_HuodongMaster.FirstOrDefault(t => t.huodong_id == id);
            if (entity != null)
            {
                entity.is_del = 1;
                if (dbcontext.SaveChanges() > 0)
                {
                    resMsg.IsSuccess = true;
                    resMsg.Message = "操作成功";
                }
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        #region 活动商品管理

        [Description("活动商品列表")]
        [ViewPage]
        public ActionResult editgoods(HuodongGoodsQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();

            var goods_ids =
                from t in dbcontext.ADZ_GoodsHuodong
                where t.huodong_id.Equals(0)
                select t.goods_id;


            //条件筛选
            var queryable = dbcontext.Views_Goods.Where(t => goods_ids.Contains(t.goods_id) && t.is_del == 0);
            if (queryModel.goods_id > 0)
            {
                queryable = queryable.Where(t => t.goods_id == queryModel.goods_id);
            }
            if (queryModel.brand_id != -10)
                queryable = queryable.Where(t => t.brand_id == queryModel.brand_id);
            if (queryModel.supplier_id != -10)
                queryable = queryable.Where(t => t.supplier_id == queryModel.supplier_id);
            if (queryModel.category_id != -10)
            {
                var cateinfo = dbcontext.ADZ_GoodsCategory.FirstOrDefault(t => t.category_id == queryModel.category_id && t.is_del == 0);
                if (cateinfo != null)
                {
                    var cateids = from t in dbcontext.ADZ_GoodsCategory
                                  where t.category_code.StartsWith(cateinfo.category_code) && t.is_del.Equals(0)
                                  select t.category_id;
                    queryable = queryable.Where(t => cateids.Contains(t.category_id.Value));
                }
            }
            if (!string.IsNullOrWhiteSpace(queryModel.MPN))
                queryable = queryable.Where(t => t.MPN.StartsWith(queryModel.MPN));

            //数据统计
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            //分页
            var querylist = queryable.OrderByDescending(t => t.goods_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<Views_Goods> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<Views_Goods>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }


        [Description("编辑活动商品")]
        [HttpPost]
        public ActionResult doeditgoods(int inout, long goods_id, long huodong_id)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || goods_id <= 0 || huodong_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            if (inout == 1)//移除
            {
                var goodslist = dbcontext.ADZ_GoodsHuodong.Where(t => t.goods_id == goods_id && t.huodong_id == huodong_id).ToList();
                if (goodslist.Any())
                {
                    goodslist.ForEach(t => t.huodong_id = 0);
                }
                dbcontext.SaveChanges();
            }
            else if (inout == 2)//加入
            {
                var goodslist = dbcontext.ADZ_GoodsHuodong.Where(t => t.goods_id == goods_id && t.huodong_id == 0).ToList();
                if (goodslist.Any())
                {
                    goodslist.ForEach(t => t.huodong_id = huodong_id);
                }
                dbcontext.SaveChanges();
            }
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
        [Description("批量编辑活动商品")]
        [HttpPost]
        public ActionResult doeditmoregoods(int inout, string goods_ids, long huodong_id)
        {
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";
            if (inout == 0 || string.IsNullOrWhiteSpace(goods_ids) || huodong_id <= 0)
            {
                resMsg.Message = "参数有误";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            if (goods_ids.EndsWith(","))
                goods_ids = goods_ids.Substring(0, goods_ids.Length - 1);
            StringBuilder strb = new StringBuilder();
            string[] id_arr = Regex.Split(goods_ids, ",");
            foreach (var item in id_arr)
            {
                long goods_id = ConvertHelper.TryPraseInt(item, 0);
                if (goods_id > 0)
                {
                    if (inout == 1)//移除
                    {
                        var goodslist = dbcontext.ADZ_GoodsHuodong.Where(t => t.goods_id == goods_id && t.huodong_id == huodong_id).ToList();
                        if (goodslist.Any())
                        {
                            goodslist.ForEach(t => t.huodong_id = 0);
                        }
                        dbcontext.SaveChanges();
                    }
                    else if (inout == 2)//加入
                    {
                        var goodslist = dbcontext.ADZ_GoodsHuodong.Where(t => t.goods_id == goods_id && t.huodong_id == 0).ToList();
                        if (goodslist.Any())
                        {
                            goodslist.ForEach(t => t.huodong_id = huodong_id);
                        }
                        dbcontext.SaveChanges();
                    }
                }
            }
            resMsg.IsSuccess = true;
            resMsg.Message = "操作成功";

            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}